home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6261 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: news.unt.edu!news
  2. From: Steve Fogoros <sfogoros@hsc.unt.edu>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: 6 byte real to double; How?
  5. Date: Fri, 23 Feb 1996 09:40:02 -0800
  6. Organization: University of North Texas Health Science Center
  7. Message-ID: <312DFBF2.74BB@hsc.unt.edu>
  8. References: <3128306A.21F@hsc.unt.edu> <4gg88a$3oi@aphex.direct.ca>
  9. NNTP-Posting-Host: sfogoros.hsc.unt.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (Win16; I)
  14.  
  15. Ed Toivanen wrote:
  16. > In article <3128306A.21F@hsc.unt.edu>, sfogoros@hsc.unt.edu says...
  17. > >
  18. > >I have a lot of data files that were created by a pascal program. The
  19. > >data is stored as 'file of real' meaning that each floating point value
  20. > >is stored in six bytes. I need to access this data from a c program
  21. > >(borland). I would like any assistance you could provide. I guess what
  22. > >I need is a canned function that would take a 6 byte string (actually the
  23. > >real), and return a double.
  24. > You need to know exactly how reals are interpreted by your pascal program. The
  25. > exact handling of a float is different(X bytes on the left side of the decimal,
  26. > Y bytes for precision) even across different C compilers, so I don't think
  27. > pascal would be much different in that respect. Once you know that, the rest is
  28. > a few C line away.
  29. > EdThank you for taking some time to consider my question. I should explain 
  30. though that I am not referring to the ascii representation of floating 
  31. point values, but the binary storage format. A six byte real (48 bits) is 
  32. divided into three fields: one sign bit (s), 39 bits for the significand 
  33. (f), and 8 bits for the exponent (e). According to Borland specification 
  34. the value is calculated as:
  35.    if 0 < e <= 255 then value = (-1)**s * 2**(e-129) * (1.f),
  36.    if e == 0 then value = 0.
  37. A double has one sign bit, 52 bits for the significand (which includes a 
  38. sign bit), and 11 bits for the exponent.
  39.  
  40. The part I'm not sure about is biasing the exponents. The real is biased 
  41. by 129 and the double is biased by 1023. (I think/hope I've got that 
  42. right). So, if the real exponent is not zero, I add 894 to it and store 
  43. the result in the double exponent (11 bits). I would go with this except 
  44. some numbers convert correctly and some don't. (My testing protocol could 
  45. be in error as well.)
  46.  
  47. I thought you would be interested in a more detailed description of the 
  48. problem. Also, others may have misunderstood my original question and I 
  49. hope this clarifies things.
  50. -- 
  51. Steve Fogoros, Academic Information Coordinator
  52. University of North Texas Health Science Center
  53. 3500 Camp Bowie Boulevard, Fort Worth, Texas 76107-2699
  54. (817)-735-2162 sfogoros@hsc.unt.edu
  55.